<?php

namespace app\models;

use Yii;
use yii\db\Query;

/**
 * This is the model class for table "gky_qyh_sp_leave".
 *
 * @property integer $id
 * @property integer $proposer_id
 * @property integer $approver_id
 * @property string $department
 * @property string $business_place
 * @property string $go_time
 * @property string $back_time
 * @property string $create_time
 * @property integer $status
 * @property string $reason
 * @property srting $print_code
 * @property string $agree_time
 * 
 */
class GkyQyhSpLeave extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'gky_qyh_sp_leave';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['proposer_id', 'approver_id', 'department', 'business_place', 'go_time', 'back_time', 'create_time', 'status', 'reason'], 'required'],
            [['proposer_id', 'approver_id', 'status'], 'integer'],
            [['go_time', 'back_time', 'create_time'], 'safe'],
            [['department'], 'string', 'max' => 50],
            [['business_place', 'reason'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'proposer_id' => 'Proposer ID',
            'approver_id' => 'Approval ID',
            'department' => 'Department',
            'business_place' => 'Business Place',
            'go_time' => 'Go Time',
            'back_time' => 'Back Time',
            'create_time' => 'Create Time',
            'status' => 'Status',
            'reason' => 'Reason',
        ];
    }

    public static function getLeave($printCode) {
        return (new Query())
                        ->select('u.avatar,u.name,l.*,ul.signature')
                        ->from('gky_qyh_sp_leave as l')
                        ->leftJoin('gky_qyh_user as u','l.proposer_id=u.id')
                        ->leftJoin('gky_qyh_user as ul','l.approver_id=ul.id')
                        ->where(['print_code' => $printCode])
                        ->one();
    }
    public static function agreeLeave($printCode,$opinion){
        $model=  GkyQyhSpLeave::findOne(['print_code'=>$printCode]);
        $model->status=1;
        $model->opinion=$opinion;
        $model->agree_time=date('Y-m-d H:m:s');
        return $model->save();
    }
    public static function refuseLeave($printCode,$opinion){
        $model=  GkyQyhSpLeave::findOne(['print_code'=>$printCode]);
        $model->status=-1;
        $model->opinion=$opinion;
        $model->agree_time=date('Y-m-d H:m:s');
        return $model->save();
    }

}
